package com.atstudy.buffer;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/20 14:09
 * @copyright: 博为峰
 */
public class StringBufferDemo {
    public static void main(String[] args) {
        /*
           字符串缓冲区(字节数组byte[capacity])
           1.线程安全
           2.长度和内容可变
           3.可以存储任意类型的数据,最终都会转成字符串输出
        */
        //增
//        m1();
        //改
        m2();
        //删
//        m3();
        //查
        /*StringBuffer buffer01 = new StringBuffer();
        buffer01.append("zhangsan").append(666).append("lisi");//zhangsan666lisi
        System.out.println(buffer01.indexOf("a"));
        System.out.println(buffer01.lastIndexOf("a"));*/
    }

    private static void m3() {
        StringBuffer buffer01 = new StringBuffer();
        buffer01.append("zhangsan").append(666).append("lisi");
//        buffer01.delete(0,2);//范围删除
        buffer01.deleteCharAt(1);
        System.out.println(buffer01);
    }

    private static void m2() {
        //改
        StringBuffer buffer01 = new StringBuffer();
        buffer01.append("zhangsan");
        buffer01.append("lisi");
        buffer01.append("wangwu");
        buffer01.setCharAt(1, 'H');//改
        buffer01.replace(0, 1, "BWF");//替换
        System.out.println(buffer01.toString());
    }

    private static void m1() {
        //初始容量为 16 个字符，如果内容超过限制，扩充
        StringBuffer buffer01 = new StringBuffer();
        StringBuffer buffer02 = buffer01.append("abc");
//        System.out.println(buffer01 == buffer02); true
        StringBuffer buffer03 = buffer02.append("defghijklmnopqrstuvwxyz");
//        System.out.println(buffer02 == buffer03);
        buffer03.append(666);
        buffer03.append(true);
        buffer03.insert(1, "BWF");//插入
        System.out.println(buffer03.toString());//buffer03.toString()
    }
}
